<?php
namespace app\system\controller;

use app\common\model\ShopExtendItemModel;
use app\common\model\ShopExtendModel;
use app\common\model\ShopGoodsExtendModel;
use think\Request;

class ShopExtendController extends BaseController
{

    private $extendModel;
    private $extendItemModel;
    private $goodsExtendModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->extendModel      = new ShopExtendModel();
        $this->extendItemModel  = new ShopExtendItemModel();
        $this->goodsExtendModel = new ShopGoodsExtendModel();
    }

    /**
     * 首页
     */
    public function index()
    {
        $map        = [];
        $query      = [];
        $map['site_id'] = $this->getSiteId();
        $query['site_id'] = $this->getSiteId();
        $extendName = $this->request->param('extend_name');
        if (!empty($extendName)) {
            $map['extend_name']   = ['like', '%' . $extendName . '%'];
            $query['extend_name'] = $extendName;
        }
        $list = $this->extendModel->where($map)->order('extend_sort asc')->paginate(10, false, [
            'query' => $query,
        ]);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $data['site_id'] = $this->getSiteId();
            $result = $this->validate($data, 'ShopExtendValidate');
            if ($result !== true) {
                $this->error($result);
            }
            //主表数据
            $this->extendModel->addData($data);
            //从表数据
            if ($this->request->has('item')) {
                $item = $this->request->param('item/a');
                foreach ($item as $k => $v) {
                    $item[$k]['extend_id'] = $this->extendModel->extend_id;
                }
                $this->extendItemModel->saveAll($item);
            }
            $this->success('添加成功', url('index'));
        }
        return $this->fetch();
    }

    /**
     * 修改
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'ShopExtendValidate');
            if ($result !== true) {
                $this->error($result);
            }
            //主表数据
            $this->extendModel->editData($data);
            //从表数据
            if ($this->request->has('item')) {
                $item = $this->request->param('item/a');
                foreach ($item as $k => $v) {
                    if ($v['item_id'] == 0) {
                        unset($item[$k]['item_id']);
                    }
                    $item[$k]['extend_id'] = $this->extendModel->extend_id;
                }
                $this->extendItemModel->saveAll($item);
            }
            $this->success('操作成功！', url('index'));
        }
        $extendId = $this->request->param('extend_id');
        $info     = $this->extendModel->where('extend_id', $extendId)->find();
        $itemList = $this->extendItemModel->where('extend_id', $extendId)->order('item_sort asc')->select();
        $this->assign('itemList', $itemList->toJson());
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $extendId = $this->request->param('extend_id/d');
            if ($this->goodsExtendModel->where('extend_id', $extendId)->find()) {
                $this->error('已有商品使用该扩展属性，不允许删除！');
            }
            $this->extendModel->where('extend_id', $extendId)->delete();
            $this->extendItemModel->where('extend_id', $extendId)->delete();
            $this->success('删除成功！');
        }
    }

    /**
     * 删除属性值
     */
    public function delItem()
    {
        $itemId = $this->request->param('item_id/d');
        if ($this->goodsExtendModel->where('item_id', $itemId)->find()) {
            $this->error('已有商品使用该属性值，不允许删除！');
        }
        $this->extendItemModel->where('item_id', $itemId)->delete();
        $this->success('操作成功！');
    }

    /**
     *排序
     */
    public function updateOrder()
    {
        $listOrder = $this->request->param('list_order/a');
        foreach ($listOrder as $extend_id => $extend_sort) {
            $extendId   = intval($extend_id);
            $extendSort = intval($extend_sort);
            $dataList[] = ["extend_id" => $extendId, "extend_sort" => $extendSort];
        }
        $this->extendModel->saveAll($dataList);
        $this->success('排序成功！');
    }

}
